﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using DG.Tweening;

public class Boss1Object : EnemyObject
{
    public AudioSource moveAudio;
    public GameObject DizzyImage;
    // Start is called before the first frame update
    protected new void Start()
    {
        slot.gridManager.Player.ListAdd(this);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    protected override void OnDestroy()
    {
        //base.OnDestroy();
        slot.gridManager.Player.ListRemove(this);
        GameObject boss1Copy = Instantiate(gameObject, slot.transform);
        boss1Copy.SetActive(true);
        boss1Copy.transform.DOMove(new Vector3(transform.position.x, slot.gridManager.transform.position.y - 3f, 0), 0.3f).SetEase(Ease.InBack);
    }

    public override bool OnPlayerTouch(Player player, int dx = 0, int dy = 0) {
        player.Hurt(1);
        //Destroy(gameObject, 0.3f);
        return true;
    }

    private int chargeTime = 1;
    private Tuple<int, int> dr;
    public override bool MoveToPlayer() {
        if(chargeTime > 1) {
            chargeTime--;
            return false;
        }
        else if(chargeTime == 1) {
            DizzyImage.SetActive(false);
            dr = slot.gridManager.MoveToPlayerBoss1(this);
            Vector3 targetAngle;
            if(dr.Item1 == 0) {
                if(dr.Item2 == 1)
                    targetAngle = new Vector3(0, 0, 90f);
                else
                    targetAngle = new Vector3(0, 0, 270f);
            }
            else if(dr.Item1 == 1) {
                targetAngle = new Vector3(0, 0, 0f);
            }
            else {
                targetAngle = new Vector3(0, 0, 180f);
            }
            transform.DORotate(targetAngle, 0.3f);
            chargeTime--;
            return false;
        }
        else if(chargeTime == 0) {
            chargeTime = 1;
            if(dr.Item1 == 0 && dr.Item2 == 0)
                return false;
            moveAudio.Play();
            if(slot.gridManager.Boss1Destroying(this, dr.Item1, dr.Item2)) {
                PureMove(dr.Item1, dr.Item2);
                slot.gridManager.EliminateTeris(false);
                return true;
            }
            else {
                return false;
            }
        }
        return false;
    }

    public void BeDizzy() {
        chargeTime = 2;
        DizzyImage.SetActive(true);
        DizzyImage.transform.position = transform.position + new Vector3(0, 1.3f * 0.4f, 0);
        DizzyImage.transform.rotation = Quaternion.identity;
    }
}
